// pages/address-edit/address-edit.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    mobile:'',
    detail:'',
    region: ['广东省', '广州市', '海珠区'],
    openid:'',
    id:'',
  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  }, 
  bindMobileChange: function(e){
    if (!(/^1[34578]\d{9}$/.test(e.detail.value))){
      wx.showToast({
        title: '手机号格式有误',
        image:'../../images/error.png',
        duration:1000,
      })
      return
    }
    this.setData({
      mobile: e.detail.value
    })
  },
  bindDetailChange: function (e) {
    this.setData({
      detail: e.detail.value
    })
  },
  bindNameChange: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  // 保存收货地址
  saveAddress:function(){
    wx.request({
      url: app.globalData.api + '&r=member.address.submit',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data:{
        address: this.data.detail,
        realname: this.data.name,
        mobile: this.data.mobile,
        province: this.data.region[0],
        city: this.data.region[1],
        area: this.data.region[2],
        openid: this.data.openid,
        id: this.data.id,
      },
      success: function (res) {
        if(res.data.error==-2){
          wx.showModal({
            title: '提示',
            content: res.data.message,
            showCancel:false,
          })
          return
        }else{
          wx.showModal({
            title: '提示',
            content: '设置成功',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } 
            }
          })
          return
        }
      },
      error: function () {
        console.log('请求错误');
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'openId',
      success(res) {
        that.setData({
          openid: res.data
        })
        if (options.id != undefined) {
          that.setData({
            id: options.id
          })
          wx.request({
            url: app.globalData.api + '&r=member.address.get_detail',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              id: options.id,
              openid: res.data
            },
            success: function (res) {
              that.setData({
                name: res.data.detail.realname,
                mobile: res.data.detail.mobile,
                detail: res.data.detail.address,
                region: [res.data.detail.province, res.data.detail.city, res.data.detail.area],
              })
            },
            error: function () {
              console.log('请求错误');
            }
          })
        }
      },
      fail() {
        wx.navigateTo({
          url: '/pages/login/login'
        });
      }
    }) 
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})